<?php
namespace Home\Controller;
use Think\Controller;
use Home\Event\WeixinEvent;
//set_time_limit(100);
class WeixinController extends Controller {
    //static $error = 0;
    public function index(){
        //获取access_token
        $access_token = accessToken();
        dump($access_token);die;
//        $getUrl = "https://api.weixin.qq.com/cgi-bin/getcallbackip?access_token={$access_token}";
//        $data = get_oneself($getUrl);
       /* $obj = M('b2c_temp_user')->select();
        dump($obj);*/
        $barcodeArr = array(
            'barcode_name' => '智慧门店1',
            'b_parameter' => 'yc_123',
            'local_id' => '3',
        );
        $code = $this->twoDBarcode($barcodeArr);
        //dump($code);die;

       /* $data = A('Weixin','Event')->weiXinUserName($access_token,'oXj1Ew_ha28bRS5TEvOqdvM330t0');
        dump($data);*/
       /* $obj = M('b2c_temp_user');
        $arr = array(
            'source_id' => 1,
            'nickname' => '猿粪', //后面请求微信接口获取用户信息
            'weixin_num' => '测试',
            'regtime' => time(),
        );
        $stat = $obj->add($arr);dump($stat);die;*/
        //dump($access_token);
        //$this->caidan($access_token);
        //$code = 'gQH98DwAAAAAAAAAAS5odHRwOi8vd2VpeGluLnFxLmNvbS9xLzAyTnVsMjVadVpkNDMxMDAwMGcwN0QAAgTpIT1YAwQAAAAA';
        //$code = 'gQGf8DoAAAAAAAAAASxodHRwOi8vd2VpeGluLnFxLmNvbS9xL2VUaXJRbURtMzhPbVlsQzd2UkNhAAIEiP47WAMEAAAAAA==';
        //$code = urlencode($code);
        //dump($code);die;
        $getUrl = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=".$code;
        //$getUrl = "https://www.baidu.com";
        // $data = file_get_contents($getUrl);
        //echo "<img style='-webkit-user-select: none; cursor: zoom-in;' src='{$getUrl}' width='369' height='369'>";
        redirect($getUrl);
        /*$acquire = A('Weixin','Event')->acquireTemplate('OPENTM406445392');
        $cs = array(
            'touser'=>'oXj1Ew_ha28bRS5TEvOqdvM330t0',
            'template_id'=>$acquire['template_id'],
            'url'=>'https://www.baidu.com',
            'data'=>[
                'first'=>[
                    'value'=>'您好，您的订单已发货',
                    'color'=>"#173177",
                ],
                "keyword1"=>[
                    "value"=>"怡臣电商",
                    "color"=>"#173177",
                ],
                "keyword2"=>[
                    "value"=>"6520214524563214",
                    "color"=>"#173177",
                ],
                "keyword3"=>[
                    "value"=>"中通",
                    "color"=>"#173177",
                ],
                "keyword4"=>[
                    "value"=>"765015236547",
                    "color"=>"#173177",
                ],
                "keyword5"=>[
                    "value"=>"请注意查收！",
                    "color"=>"#173177",
                ],
            ],
        );
        $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$access_token;
        $msg = A('Weixin','Event')->parameterPostBtn($cs,$url);
        dump($msg);*/
        die;

    }



    /**
     * @param $access_token
     * $barcodeArr 数组
     * @return mixed
     * 生成带参二维码
     */
    public function twoDBarcode($barcodeArr){
        $obj = A('Weixin','Event');
        $data = $obj->twoDBarcode($barcodeArr);
        //dump($data);die;
        if(isset($data['errcode'])){
            return $data;
        }
        $barcodeArr['barcode_img'] = $data['ticket'];
        $barcodeArr['regtime'] = time();
        $stat = M('b2c_2d_barcode')->data($barcodeArr)->add();
        return $stat === false ? false : $data['ticket'];
    }

    /**
     * 获取微信用户信息
     * @param $access_token
     * @param $fromUserName
     * @return bool|mixed
     */
    public function weiXinUserName($access_token,$fromUserName){
        $getUrl = "https://api.weixin.qq.com/cgi-bin/user/info?access_token={$access_token}&openid={$fromUserName}&lang=zh_CN ";
        $data = get_oneself($getUrl);
        if(isset($data['errcode'])){
            return false;
        }
        return $data;
    }

    /**
     * 菜单
     */
    public function caidan(){
        $wx_url = C('WX_URL');
        //$getUrl = 'https://api.weixin.qq.com/cgi-bin/menu/create?access_token='.$access_token;

        $params = array(
            'button' => array(
                /*
                array('name' => '菜单','sub_button' => array(array('type' => 'view','name' => '以诚优选','url' => 'http://weixin.yichen123.cn/index.php/home/First/index'),
                                                        array('type' => 'view','name' => '视频','url' => 'http://v.qq.com/'),
                    )),*/

                /*array('type' => 'view','name' => '怡臣优选','url' => $wx_url.'Home/First/index '),
                array('type' => 'view','name' => '个人中心','url' => $wx_url.'Home/First/user '),
                array('type' => 'view','name' => '购物车','url' => $wx_url.'Home/First/carts '),*/

                array('type' => 'view','name' => '怡臣优选','url' => $wx_url.'Home/First/index'),
                array('name' => '我的','sub_button' => array(array('type' => 'view','name' => '个人中心','url' => $wx_url.'Home/First/user'),array('type' => 'view','name' => '购物车','url' => $wx_url.'Home/First/carts'))),
                array('type' => 'view','name' => '获取奖金','url' => $wx_url.'Home/First/chooseShop'),

            )
        );

        $obj = A('Weixin','Event');
        $data = $obj->caidan($params);
        dump($data);
    }
  

    

   

}